/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.risk.hid.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.risk.common.constant.HidDicConstant;
import com.hopes.bizz.risk.hid.dto.RiskHidSuperviseDTO;
import com.hopes.bizz.risk.hid.entity.RiskHidHiddenDangerEntity;
import com.hopes.bizz.risk.hid.entity.RiskHidSuperviseEntity;
import com.hopes.bizz.risk.hid.mapper.RiskHidSuperviseMapper;
import com.hopes.bizz.risk.hid.service.RiskHidSuperviseService;
import com.hopes.boss.common.message.distribute.AppMessageBO;
import com.hopes.boss.common.message.distribute.IMessageDistributor;
import com.hopes.boss.common.message.distribute.MessageSendBO;
import com.hopes.boss.common.message.distribute.WebMessageBO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 隐患督办
 *
 * @author lyb
 * @date 2024-02-22 15:34:47
 */
@Service
@AllArgsConstructor
@Slf4j
public class RiskHidSuperviseServiceImpl extends ServiceImpl<RiskHidSuperviseMapper, RiskHidSuperviseEntity> implements RiskHidSuperviseService {

    private final RiskHidHiddenDangerServiceImpl riskHidHiddenDangerService;

    private final IMessageDistributor messageDistributor;

    @Override
    public void supervise(RiskHidSuperviseDTO riskHidSuperviseDTO) {
        // 推送消息
        MessageSendBO messageSendBO = new MessageSendBO();
        String content = riskHidSuperviseDTO.buildContent();
        if (riskHidSuperviseDTO.appWay()) {
            AppMessageBO appMessageBO = new AppMessageBO();
            appMessageBO.setTitle(riskHidSuperviseDTO.buildTitle());
            appMessageBO.setMessageText(content);
            appMessageBO.setReceiveUserIds(riskHidSuperviseDTO.getReceiver());
            messageSendBO.addMessage(appMessageBO);
        }
        if (riskHidSuperviseDTO.webWay()) {
            WebMessageBO webMessageBO = new WebMessageBO();
            webMessageBO.setNeedBroadcast(false);
            List<String> receiverList = Arrays.stream(riskHidSuperviseDTO.getReceiver().split(",")).collect(Collectors.toList());
            webMessageBO.setSessionKeys(new ArrayList<>(receiverList));
            webMessageBO.setMessageText(content);
            messageSendBO.addMessage(webMessageBO);
        }


        boolean success = true;
        try {
            messageDistributor.distribute(messageSendBO);
        } catch (Exception e) {
            log.error("隐患督办消息推送失败,原因: {}", e.getMessage());
            success = false;
        }
        // 保存
        Long hidId = riskHidSuperviseDTO.getHidId();
        RiskHidHiddenDangerEntity hidHiddenDanger = riskHidHiddenDangerService.getById(hidId);
        Long orgId = hidHiddenDanger.getOrgId();
        RiskHidSuperviseEntity riskHidSupervise = riskHidSuperviseDTO.toEntity();
        riskHidSupervise.setOrgId(orgId);
        riskHidSupervise.setSuperviseStatus(success ? HidDicConstant.RISK_HID_SUPERVISE_STATUS_SUCCESS : HidDicConstant.RISK_HID_SUPERVISE_STATUS_FAIL);
        save(riskHidSupervise);
    }
}